package com.lb.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisSubListenerConfig {
    @Autowired
    private MessageProperties messageProperties;
    @Bean
    public RedisMessageListenerContainer container(@Autowired RedisConnectionFactory redisConnectionFactory,
                                                   MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        redisMessageListenerContainer.addMessageListener(listenerAdapter, new PatternTopic(messageProperties.getSubTopicName()));
        redisMessageListenerContainer.setTopicSerializer(new StringRedisSerializer());
        return redisMessageListenerContainer;
    }
    @Bean
    MessageListenerAdapter listenerAdapter(RedisSubReceiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }
}
